# SNR在不同RCS下与距离的关系
from typing import List, Dict
import numpy as np
from core.radar_core import RadarCore
from apps.cpm.rlm_context import RlmContext

class SnrRcssRangesAgent(object):
    def __init__(self):
        self.name = 'apps.cpm.agent.snr_rcss_ranges_agent.SnrRcssRangesAgent'

    @staticmethod
    def run(context:RlmContext) -> Dict:
        observes = SnrRcssRangesAgent._observe(context=context)
        plans = SnrRcssRangesAgent._reason(observes=observes, context=context)
        return SnrRcssRangesAgent._act(observes=observes, context=context, plans=plans)

    def _observe(context:RlmContext) -> Dict:
        observes = {}
        observes['pt'] = context.states['params']['pt']
        observes['freq'] = context.states['params']['freq']
        observes['g'] = context.states['params']['g']
        observes['sigma'] = context.states['params']['sigma']
        observes['te'] = context.states['params']['te']
        observes['b'] = context.states['params']['b']
        observes['nf'] = context.states['params']['nf']
        observes['loss'] = context.states['params']['loss']
        observes['range_values'] = context.states['params']['range_values']
        observes['sigma_values'] = context.states['params']['sigma']
        return observes
    
    def _reason(observes:Dict, context:RlmContext) -> List:
        plans = []
        return plans
    
    def _act(observes:Dict, context:RlmContext, plans:List) -> Dict:
        context.states['params']['snr_rcss_ranges'] = np.array([RadarCore.radar_eq(observes['pt'][0], observes['freq'], observes['g'], s, observes['te'], observes['b'], observes['nf'], observes['loss'], observes['range_values']) 
                            for s in observes['sigma_values']])
        if 'ys' not in context.states['params']:
            context.states['params']['ys'] = [{
                'data': context.states['params']['snr_rcss_ranges'],
                'title': '信噪比与距离曲线（不同RCS）',
                'labels': [r'$\sigma$ = 0 dBsm', 
                r'$\sigma$ = -10 dBsm', 
                r'$\sigma$ = +10 dBsm']
            }]
        else:
            context.states['params']['ys'].append({
                'data': context.states['params']['snr_rcss_ranges'],
                'title': '信噪比与距离曲线（不同RCS）',
                'labels': [r'$\sigma$ = 0 dBsm', 
                r'$\sigma$ = -10 dBsm', 
                r'$\sigma$ = +10 dBsm']
            })
        return {'result': 0, 'params': {'snr_rcss_ranges': context.states['params']['snr_rcss_ranges']}}